package com.atguigu.yygh.controller;

import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.service.HospitalSetService;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "1.医院管理设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalset")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    //1.查询所有医院的操作,改造之后的方法,等学习过springcatch和redis缓存后，我们可以把这个也用一下缓存,别忘了配redis
//    @Cacheable(value = "hosptial",keyGenerator = "keyGenerator")
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")
    public Result findAllHosptialSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
//    public List<HospitalSet> findAllHosptialSet(){
//        List<HospitalSet> list = hospitalSetService.list();
//        return list;
//    }


    //2.删除医院操作,这种方式浏览器没办法直接访问，所以我们采用swagger2这个工具类
    //具体的配置在common模块service_utils里，可以看看。具体的包为com.atguigu.yygh.common.config。Swagger2Config
    //但是返回数据不一致会导致开发人员很难配合，所以定义统一返回数据类型，在common模块common_utils,别忘了导入依赖包common_utils
    @ApiOperation(value = "医院逻辑删除设置")
    @DeleteMapping("/delete/{id}")
    public Result removeHospSet(@PathVariable("id") Long id){
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return Result.ok();
        }else return Result.fail();
    }
//    未改造之前的代码，
//    public boolean removeHospSet(@PathVariable("id") Long id){
//        boolean b = hospitalSetService.removeById(id);
//        return b;
//    }



    //3.医院列表(条件查询分页)
    @ApiOperation(value = "条件查询带分页医院设置信息")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  //对于条件一般用json进行传递，前端方便，直接对数据对象进行赋值，必须用RequestBody注解，切方法要变为PostMapping
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建查询条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname ",hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode ",hospitalSetQueryVo.getHoscode());
        }
        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);

        //返回结果
        return Result.ok(hospitalSetPage);

    }

    //4 添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {

        //这一段不注释掉会影响我们后面的redis
//        try{
//            int i=1/0;
//        }catch (Exception e){
//            throw new YyghException("失败",201);
//        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //8 医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9 发送签名秘钥
//    @ApiOperation(value = "发送签名秘钥")
//    @PutMapping("sendKey/{id}")
//    public Result lockHospitalSet(@PathVariable Long id) {
//        HospitalSet hospitalSet = hospitalSetService.getById(id);
//        String signKey = hospitalSet.getSignKey();
//        String hoscode = hospitalSet.getHoscode();
//        return Result.ok();
//    }

}
